1
|
|
|
"use strict"; |
2
|
|
|
|
3
|
|
|
if (['dev', 'prod'].indexOf(process.env.NODE_ENV)) { |
4
|
|
|
console.warn(`Undefined node environment "${process.env.NODE_ENV}", fallback to dev !`); |
5
|
|
|
process.env.NODE_ENV = 'dev'; |
6
|
|
|
} |
7
|
|
|
|
8
|
|
|
const logger = require('./lib/logger'); |
9
|
|
|
const server = require('./lib/server'); |
10
|
|
|
const Alfred = require('./lib/Alfred'); |
11
|
|
|
const Client = require('./lib/Client'); |
12
|
|
|
const PluginManager = require('./lib/PluginManager'); |
13
|
|
|
const pkg = require('./package.json'); |
14
|
|
|
const NestedError = require('nested-error-stacks'); |
15
|
|
|
|
16
|
|
|
const appName = pkg.name; |
17
|
|
|
|
18
|
|
|
const alfred = new Alfred( |
19
|
|
|
new Client(), |
20
|
|
|
new PluginManager([]) |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
const cleanAndExit = (exitCode = 0) => { |
|
|
|
|
24
|
|
|
let canExit = false; |
25
|
|
|
const waitAndExit = arg => { |
26
|
|
|
|
27
|
|
|
if (arg instanceof Error) { |
28
|
|
|
exitCode = 1; |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if (canExit === false) { |
32
|
|
|
setTimeout(waitAndExit, 1000); |
33
|
|
|
} else { |
34
|
|
|
process.exit(); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
}; |
37
|
|
|
|
38
|
|
|
const stopAlfred = arg => { |
39
|
|
|
|
40
|
|
|
if (arg instanceof Error) { |
41
|
|
|
exitCode = 1; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
logger.stopping('Alfred'); |
44
|
|
|
return alfred.sleep() |
45
|
|
|
.then(() => logger.stopped('Alfred')); |
46
|
|
|
}; |
47
|
|
|
const updateCleanState = arg => { |
48
|
|
|
if (arg instanceof Error) { |
49
|
|
|
exitCode = 1; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
canExit = true; |
52
|
|
|
}; |
53
|
|
|
server.stop() |
54
|
|
|
.then(stopAlfred, stopAlfred) |
55
|
|
|
.then(updateCleanState, updateCleanState) |
56
|
|
|
}; |
57
|
|
|
|
58
|
|
|
process.on('SIGINT', cleanAndExit); |
59
|
|
|
|
60
|
|
|
logger.starting(appName); |
61
|
|
|
return alfred.wakeUp() |
62
|
|
|
.then(() => server.start()) |
63
|
|
|
.then(() => logger.started(appName)) |
64
|
|
|
.catch(error => { |
65
|
|
|
const newError = new NestedError(`Exit ${appName} after an error at initialisation`, error); |
|
|
|
|
66
|
|
|
logger.error(error.stack); |
67
|
|
|
cleanAndExit(1); |
68
|
|
|
}); |
69
|
|
|
|
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.